package com.ggs.controller;

import com.ggs.pojo.Category;
import com.ggs.service.CategoryService;
import com.ggs.utils.ThreadLocalUtils;
import com.ggs.vo.ResultVO;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
// 新增文章分类
    @PostMapping
    public ResultVO add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return ResultVO.success();
    }

//    获取文章分类列表
    @GetMapping
    public ResultVO list(){
        Map claims = ThreadLocalUtils.get();
        Integer id = (Integer) claims.get("id");
        List<Category> categoryList = categoryService.findAll(id);
        return ResultVO.success(categoryList);
    }

//    根据ID获取文章分类详情
    @GetMapping("/detail")
    public ResultVO detailById(@NotNull Integer id){
        Category category = categoryService.findById(id);
        return ResultVO.success(category);
    }

//    更新文章分类
    @PutMapping
    public ResultVO update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return ResultVO.success();
    }
//    根据ID删除分类
    @DeleteMapping
    public ResultVO delete(@NotNull  Integer  id){
        System.out.println("+++++++"+id);
        categoryService.delete(id);
        return ResultVO.success();
    }
}
